package com.bv.hr.system.util;

import com.alibaba.fastjson.JSONObject;
import com.bv.hr.common.core.http.HttpResult;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author CYT7
 * @version 1.0
 * @time 12月 20,2021
 * @describe Http工具类
 */
public class HttpUtils {
    /**
     * @describe 获取HttpServletRequest对象
     * @return 返回HttpServletRequest对象
     */
    public static HttpServletRequest getHttpServletRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    /**
     * @describe 输出信息到游览器
     * @param response HttpServlet响应
     * @param code 响应码
     * @param msg 信息
     * @throws IOException
     */
    public static void print(HttpServletResponse response, int code, String msg) throws IOException {
        HttpResult result = HttpResult.error(code, msg);
        String json = JSONObject.toJSONString(result);
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().print(json);
        response.getWriter().flush();
        response.getWriter().close();
    }
}
